package com.cloud.base.user.server.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cloud.base.user.server.dao.UserDao;
import com.cloud.base.user.server.util.CommonUtil;
import com.cloud.base.user.server.util.HibernateBasic;
import com.cloud.base.user.server.util.PageBean;
import com.sql.generator.Query;
import com.sql.generator.SQL;
import com.sql.generator.Service;
import com.sql.generator.Util;

@Component
public class UserDaoImpl implements UserDao {
	
    @Autowired
    private SessionFactory sessionFactory;
    
	@SuppressWarnings("unchecked")
	public Map<String, Object> queryUserList(Map<String, Object> requestMap, PageBean page) {
		Query mainQuery = SQL.select("USER_ID,NAME,AGE,SEX,USER_IMG,STATUS");
		mainQuery.from("t_user");
		mainQuery.setGenerateCountQuery(true);
		if (page != null) {
			mainQuery.limit(page.getOffset(), page.getPageSize());
		}
		Service service = new Service(mainQuery);
		service.useCustomResultTransformer();
		service.setDescription(this.getClass() + ".queryUserList()");
		Map<String, Object> result = new HibernateBasic(sessionFactory).execute(service);
		if (CommonUtil.isInvalid(result)) {
			return null;
		}
		List<Map<String, Object>> list = (List<Map<String, Object>>) result.get(mainQuery.getResultCallback());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("LIST", list);
		resultMap.put("TOTAL_NUM", Util.getTotalNumStr(result, mainQuery));
		return resultMap;
	}
	
}
